﻿package com.sh.game.loader
{
    import com.core.*;
    import com.sh.game.consts.*;
    import com.sh.game.global.*;
    import flash.utils.*;
    import morn.core.handlers.*;

    public class SceneProloaderManager extends Object
    {
        private var _cfgs:Array;
        private var _caches:Array;
        private var _running:Boolean = false;
        private var curloading:Object;
        private var _totalNum:int = 0;
        private var _handler:Function;
        private var _urls:Array;
        public var noNeed:Dictionary;
        private var _preloadAdd:Boolean = false;
        private var _moduleAdd:Boolean = false;
        private static var _instance:SceneProloaderManager;
        public static const TYPE_WOMAN_MODEL:int = 0;
        public static const TYPE_MAN_MODEL:int = 1;
        public static const TYPE_MONSTER_MODEL:int = 2;
        public static const TYPE_NPC_MODEL:int = 3;
        public static const TYPE_MAP:int = 4;
        public static const TYPE_SWF:int = 5;
        public static const TYPE_ARM:int = 6;
        public static const TYPE_WING:int = 7;

        public function SceneProloaderManager()
        {
            this.noNeed = new Dictionary();
            this._caches = new Array();
            this._caches[TYPE_WOMAN_MODEL] = new Dictionary();
            this._caches[TYPE_MAN_MODEL] = new Dictionary();
            this._caches[TYPE_MONSTER_MODEL] = new Dictionary();
            this._caches[TYPE_NPC_MODEL] = new Dictionary();
            this._caches[TYPE_MAP] = new Dictionary();
            this._caches[TYPE_SWF] = new Dictionary();
            return;
        }// end function

        public function get running() : Boolean
        {
            return this._running;
        }// end function

        public function getCacheAndClear(param1:int) : Object
        {
            var _loc_2:* = this._caches[param1];
            this._caches[param1] = null;
            return _loc_2;
        }// end function

        public function getCache(param1:int) : Object
        {
            var _loc_2:* = this._caches[param1];
            return _loc_2;
        }// end function

        public function start() : void
        {
            if (!this._preloadAdd)
            {
                this._cfgs = ModelProxy.getValue("data", "configs1.preload");
                this._running = true;
                this._preloadAdd = true;
                this._totalNum = this._totalNum + this._cfgs.length;
                this._urls = this._urls.concat(this._cfgs);
                if (!this.curloading)
                {
                    this.loadNext();
                }
            }
            return;
        }// end function

        public function startModule(param1:Array) : void
        {
            if (!this._moduleAdd)
            {
                this._moduleAdd = true;
                this._running = true;
                this._urls = param1;
                this._totalNum = this._urls.length;
                if (!this.curloading)
                {
                    this.loadNext();
                }
            }
            return;
        }// end function

        private function loadNext() : void
        {
            if (this._urls && this._urls.length > 0)
            {
                if (this._running)
                {
                    this.curloading = this._urls.shift();
                    if (this.curloading.type == TYPE_WOMAN_MODEL || this.curloading.type == TYPE_MAN_MODEL)
                    {
                        App.mloader3.loadBYTE(Config.getUrl(Config.clothSkinPath + this.curloading.url + Config.SkinExt), 1, new Handler(this.completeHandler), null, null, false);
                    }
                    else if (this.curloading.type == TYPE_MONSTER_MODEL)
                    {
                        App.mloader3.loadBYTE(Config.getUrl(Config.monsterPath + this.curloading.url + Config.SkinExt), 1, new Handler(this.completeHandler), null, null, false);
                    }
                    else if (this.curloading.type == TYPE_NPC_MODEL)
                    {
                        App.mloader3.loadBYTE(Config.getUrl(Config.npcPath + this.curloading.url + Config.SkinExt), 1, new Handler(this.completeHandler), null, null, false);
                    }
                    else if (this.curloading.type == TYPE_MAP)
                    {
                        App.mloader3.loadBMD(Config.getUrl(Config.mapPath + Config.NewBirdCity + "/" + this.curloading.url + Config.JPGExt), 1, new Handler(this.completeHandler), null, null, false);
                    }
                    else if (this.curloading.type == TYPE_SWF)
                    {
                        if (this.noNeed[this.curloading.url])
                        {
                            this.curloading = null;
                            this.loadNext();
                            return;
                        }
                        App.mloader3.loadBYTE(Config.getUrl(this.curloading.url, ResourceType.SWF), 1, new Handler(this.completeHandler), null, null, false);
                    }
                }
            }
            else
            {
                this._running = false;
                if (this._handler != null)
                {
                    this._handler(this.getCacheAndClear(SceneProloaderManager.TYPE_WOMAN_MODEL), this.getCacheAndClear(SceneProloaderManager.TYPE_MAN_MODEL), this.getCacheAndClear(SceneProloaderManager.TYPE_MONSTER_MODEL), this.getCacheAndClear(SceneProloaderManager.TYPE_NPC_MODEL));
                }
            }
            return;
        }// end function

        private function completeHandler(param1) : void
        {
            if (this.curloading && this._caches[this.curloading.type])
            {
                this._caches[this.curloading.type][this.curloading.url] = param1;
                this.curloading = null;
            }
            this.loadNext();
            return;
        }// end function

        public function end() : void
        {
            this._urls.length = 0;
            if (this.curloading)
            {
                this.curloading = null;
            }
            this._running = false;
            return;
        }// end function

        public function pause() : void
        {
            this._running = false;
            return;
        }// end function

        public function initCache(param1:Function) : void
        {
            this._handler = param1;
            if (param1 != null && !this._running)
            {
                this.param1(this.getCacheAndClear(SceneProloaderManager.TYPE_WOMAN_MODEL), this.getCacheAndClear(SceneProloaderManager.TYPE_MAN_MODEL), this.getCacheAndClear(SceneProloaderManager.TYPE_MONSTER_MODEL), this.getCacheAndClear(SceneProloaderManager.TYPE_NPC_MODEL));
            }
            return;
        }// end function

        public static function get instance() : SceneProloaderManager
        {
            if (_instance == null)
            {
                _instance = new SceneProloaderManager;
            }
            return _instance;
        }// end function

    }
}
